bitmasks dfs and similar graphs *1700

Please click on ads to support us..

Python Code:

from collections import Counter
import sys, threading 

input = lambda: sys.stdin.readline().strip()

def main():

    for _ in range(int(input())):

        n, a, b = map(int, input().split())
        
        adj = [[] for _ in range(n + 1)]
        for _ in range(n - 1):
            u, v, w = map(int, input().split())
            adj[u].append((v, w))
            adj[v].append((u, w))

        ok, cnt = False, Counter()
        
        def dfs1(u, p, xor):
            
            cnt[xor] += 1
            for v, w in adj[u]:
                if v == p:
                    continue
                dfs1(v, u, xor ^ w)
        
        for v, w in adj[b]:
            dfs1(v, b, w)

        def dfs2(u, p, xor):
            nonlocal ok 

            if u == b:
                return 

            if cnt[xor]:
                ok = True 
                return 

            for v, w in adj[u]:
                if v == p:
                    continue
                dfs2(v, u, xor ^ w)
        
        dfs2(a, -1, 0)

        print("YES" if ok else "NO")

if __name__ == '__main__':
    
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()
    
    

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define pb push_back
#define sz size()
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
void start(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const ll N = 1e5+10;
const ll mod=1e9+7;
const ll inf=1e10;
map<ll,ll> mp;
vector <vector <pair<ll,ll> > > g(N);
ll x,y;
bool flag=false;
void dfs(ll v, ll pr,ll k){
    if(v==y && k!=0) return;
    else if(v==y) flag=true;
    mp[k]++;
    for(auto i : g[v]){
        if(i.fr==pr) continue;
        dfs(i.fr,v,k^i.sc);
    }
}
bool dfs2(ll v,ll pr,ll k){
    if(v!=y && mp[k]>0){
        return true;
    }
    for(auto i : g[v]){
        if(i.fr==pr) continue;
        if(dfs2(i.fr,v,k^i.sc)) return true;
    }
    return false;
}
void solve(){
    ll a,b,i,j;
    ll n,w;
    cin>>n>>x>>y;
    mp.clear();
    for(i=1;i<=n;i++) g[i].clear();
    for(i=1;i<n;i++){
        cin>>a>>b>>w;
        g[b].pb({a,w});
        g[a].pb({b,w});
    }
    flag=false;
    dfs(x,-1,0);
    if(dfs2(y,-1,0) || flag) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
main(){
    //fre("");
    start();
    ll t=1;
    cin>>t;
    while(t--)solve();

}


Comments

Submit
0 Comments
More Questions

236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD